#include "EditorScrollWidget.h"

#include "Widgets/Positioners/PercentPositionerWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Mouse/MouseWidget.h"

REditorScrollWidget::REditorScrollWidget()
{
    bClipSubWidgets = true;
    auto MouseWheel=NewWidget<RMouseWidget>(this);

    auto ContentPositioner = NewWidget<RPercentPositionerWidget>(this);
    ContentPositioner->Anchor.Fill(this);
    ContentPositioner->YPercent = [this]()->float
    {
        if(OnGetVerticalPercent.IsBound())
        {
            VerticalPercent=OnGetVerticalPercent.Execute(); 
        }

        return VerticalPercent;
    };

    ContentContainer = NewWidget<RWidget>(ContentPositioner);


    //vertical scroll bar
    auto VerticalScrollBar = NewWidget<RWidget>(this);
    {
        VerticalScrollBar->Anchor.RightReference = this;
        VerticalScrollBar->Anchor.TopReference = this;
        VerticalScrollBar->Anchor.BottomReference = this;
        VerticalScrollBar->Width = 20;

        auto VerticalScrollBarMouse= NewWidget<RMouseWidget>(VerticalScrollBar);
        VerticalScrollBarMouse->Anchor.Fill(VerticalScrollBar);

        auto DragBar= NewWidget<RRectangleWidget>(VerticalScrollBar);
        DragBar->FillColor = SColor(1.f,1.f,1.f,0.2f);
        DragBar->Anchor.HorizontalFill(VerticalScrollBar);
        RWidgetWeakPtr WeakScrollBar=VerticalScrollBar;
        DragBar->Y=[this,WeakScrollBar]()->float
        {
            // if(OnGetVerticalPercent.IsBound())
            // {
            //     VerticalPercent=OnGetVerticalPercent.Execute();
            // }

            float VisualHeight= this->PresentSize.Y;
            float ContentHeight=ContentContainer->PresentSize.Y;
            float DragBarHeight=VisualHeight*VisualHeight/ContentHeight;
            return (VisualHeight-DragBarHeight)*VerticalPercent;
        };
        
        DragBar->Height=[this,WeakScrollBar]()->float
        {
            //最大可见区域高度
            float VisualHeight= this->PresentSize.Y;
            //内容高度
            float ContentHeight=ContentContainer->PresentSize.Y;
            if(ContentHeight<=VisualHeight)
            {
                //内容高度小于可见区域高度，隐藏滚动条
                return 0; 
            }

            float DragBarHeight=VisualHeight*VisualHeight/ContentHeight;
            return DragBarHeight;
        };

    }



    MouseWheel->Anchor.Fill(this);
    MouseWheel->OnWheeled.BindLambda([this](const SMouseButtonEvent& Event)
    {
        // if(OnGetVerticalPercent.IsBound() )
        // {
        //     VerticalPercent=OnGetVerticalPercent.Execute();
        // }

        float Delta=Event.GetWheeledDistance()*MouseWheelSpeed.Get();
        float VisualHeight= this->PresentSize.Y;
        float ContentHeight=ContentContainer->PresentSize.Y;
        float NewVerticalPercent=VerticalPercent-Delta/ContentHeight;
        NewVerticalPercent=CMath::Clamp(NewVerticalPercent,0.f,1.f);
        if(OnSetVerticalPercent.IsBound())
        {
            OnSetVerticalPercent.Execute(NewVerticalPercent);
        }
        else
        {
            VerticalPercent=NewVerticalPercent;
        }
        return true;
    });

}

TSharedObjectPtr<RWidget> REditorScrollWidget::GetContentContainer() const
{
    return ContentContainer;
}